﻿using AutoMapper;
using Dm;
using MediatR;
using ProductionManagement.Application.Command.DispatchCommand;
using ProductionManagement.Domain.ProductionDomain;
using ProductionManagement.Infrstructre.lnterface;
using SqlSugar;

namespace ProductionManagement.Application.CommandHandler.DIspatchCommandHandlers
{
    public class CreateDispatchCommandHandlers:IRequestHandler<CreateDiapatchCommand,int>
    {

        private readonly IDispatchRepository _dispatch;
        private readonly IDisProductRepository _disProduct;
        private readonly IDisMaterialRepository _disMaterial;
        private readonly IMapper _mapper;
        ISqlSugarClient db;


        public CreateDispatchCommandHandlers(IDispatchRepository dispatch, IDisProductRepository disProduct, IDisMaterialRepository disMaterial, IMapper mapper, ISqlSugarClient db)
        {
            _dispatch = dispatch;
            _disProduct = disProduct;
            _disMaterial = disMaterial;
            _mapper = mapper;
            this.db = db;
        }

        public async Task<int> Handle(CreateDiapatchCommand request, CancellationToken cancellationToken)
        {
            db.Ado.BeginTran();
            try
            {
                //添加生产派工主表
                var dispatch = _mapper.Map<Dispatch>(request.AddDto);
                var code = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                dispatch.DNumber = $"SCPG{code}";
                var res = await _dispatch.AddAsync(dispatch);


                //添加生产派工产品表
                 
                foreach (var item in request.AddDto.DispatchProduct)
                {
                    DispatchProduct disPro = new DispatchProduct()
                    {
                        DNumber = dispatch.DNumber,
                        DDeliveryDate = item.DDeliveryDate,
                        DispatchProductId = item.DispatchProductId,
                        DPEndDate = item.DPEndDate,
                        DPStartDate = item.DPStartDate,
                        DThisNum = item.DThisNum,
                        OrderNum = item.OrderNum,
                        PName = item.PName,
                        PNumber = item.PNumber,
                        ProductId = item.ProductId,
                        PSpec = item.PSpec,
                        PUnit = item.PUnit,
                        AddName=request.AddDto.AddName,
                        AddTime=DateTime.Now,
                        Office_Id=item.Office_Id,
                        User_Id=item.User_Id,

                    };
                    await _disProduct.AddAsync(disPro);
                }



                ////添加生产派工物料表
                foreach(var item in request.AddDto.DisMaterial)
                {
                    DisMaterial disMat = new DisMaterial()
                    {
                        DNumber = dispatch.DNumber,
                        DisMaterialId = item.DisMaterialId,
                        MSubject=item.MSubject,
                        ArrivalDate = item.ArrivalDate,
                        MaterialId = item.MaterialId,
                        MPName = item.MPName,
                        MPNumber = item.MPNumber,
                        MPSpec = item.MPSpec,
                        MPUnit = item.MPUnit,
                        NeedNum = item.NeedNum,
                        PRatio = item.PRatio,
                        PStock = item.PStock,
                        Remark = item.Remark,
                        WarehouseStorageId = item.WarehouseStorageId,
                    
                    };
                    await _disMaterial.AddAsync(disMat);
                }



                db.Ado.CommitTran();
                return 1;
            }
            catch (Exception)
            {
                db.Ado.RollbackTran();
                throw;
            }
        }
    }
}
